<?php
require_once FOREGROUND_COMMON_ACTION_SERVICE_DIR . '/ajax.php';

class foreground_order_confirm_receive_action_service extends foreground_ajax_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {	     		     
	    $result = array('status' => 0, 'message' => '', 'data' => array());
        
        $id = init_user_data_key($_POST, 'id', 'int');
        
        $action_data = wwy_action::$data;
	    $session_member_info = $action_data['session_member_info'];
        $member_id = $session_member_info['id'];
                    
        if(empty($id)){
            $result['status']  = -2;
            $result['message'] = '订单错误';
        }else{    
            $model_foreground_order = wwy_resource_container::get_model_foreground_order();
            //var_dump($model_foreground_order); exit();
            
            //$old_order_info = $model_foreground_order->find($id);
            $where  = array(
                'id'        => $id,
                'member_id' => $member_id
            );
            $old_order_info = $model_foreground_order->fetch_row($where);
            //var_dump($old_order_info); exit();
            if(empty($old_order_info)){
                $result['status']  = -3;
                $result['message'] = '该订单不存在';
            }else if($old_order_info['status'] != 2){
                $result['status']  = -3;
                $result['message'] = '该订单还未发货，不能确认收货';
            }else{            
                $update_time = time();
                $order_info = array();
                $order_info['id']          = $id;
                $order_info['status']      = 3;
                $order_info['confirm_receive_time'] = $update_time;
                $order_info['update_time'] = $update_time;
            
                $write_db = $this->_get_write_db();
                //var_dump($write_db); exit();
                $pdo = $write_db->get_connection();
                //var_dump($pdo); exit();
                $pdo->beginTransaction();
            
                try{
                    $row_count = $model_foreground_order->set_confirm_received($order_info);
                    //var_dump($row_count); exit();
                     
                    if($row_count === false){
                        $result['status']  = -4;
                        $result['message'] = '确认收货失败';
                    }else{
                        $result['status']  = 1;
                        $result['message'] = '确认收货成功';
                    }
                    //var_dump($result); exit();
                     
                    $pdo->commit();
                }catch (Exception $e){
                    $pdo->rollBack();
            
                    throw $e;
                }
            }
        }            
         
        self::set_view_status($result['status']);
        self::set_view_message($result['message']);
    }
}